import userService from "../service/user.service.js";
import fileService from "../service/file.service.js";
import downloadService from "../service/download.service.js";
import shareService from "../service/share.service.js";
import getHourlyStats from "../utils/log-reader.js";
import configer from "../utils/configer.js";

import "express-async-errors";

const getTenUsers = async (req, res) => {
    const users = await userService.getTenUsers();
    res.json(users);
}

const getUsers = async (req, res) => {
    const users = await userService.getAllUsers();
    res.json(users);
}

const getOverview = async (req, res) => {
    const user = await userService.getUserIncreaseInfo();
    const file = await fileService.getFileIncreaseInfo();
    const download = await downloadService.getDownloadIncreaseInfo();
    const share = await shareService.getShareIncreaseInfo();
    res.json({
        user,
        file,
        download,
        share,
    })
}

const getRecentDays = async (req, res) => {
    const user = await userService.getRecentDaysUsers();
    const file = await fileService.getRecentDaysFiles();
    const download = await downloadService.getRecentDaysDownloads();
    const share = await shareService.getRecentDaysShares();
    res.json({
        user,
        file,
        download,
        share,
    })
}

const getFileProperty = async (req, res) => {
    const property = await fileService.getFileProperty();
    res.json(property);
}

const getLogs = async (req, res) => {
    const logs = await getHourlyStats();
    res.json(logs);
}
const updateSpace = async (req, res) => {
    const { storage, id } = req.body;
    const user = await userService.getUserById(id);
    if (!user || storage < Number(user.used)) {
        return res.status(400).json({ message: "修改的大小过小" });
    }
    await userService.updateUser(id, { storage });
    res.json({ message: "更新成功" });
}

const updateUserStatus = async (req, res) => {
    const { id, status } = req.body;
    const s = status == "ACTIVE" ? "ACTIVE" : "SUSPENDED";
    await userService.updateUser(id, { status: s });
    res.json({ message: "更新成功" });
}

const updateUserRole = async (req, res) => {
    const { id, role } = req.body;
    const r = role == "ADMIN" ? "ADMIN" : "USER";
    await userService.updateUser(id, { role: r });
    res.json({ message: "更新成功" });
}

const getConfig = async (req, res) => {
    const defaultUserSpace = configer.get("defaultUserSpace");
    const uploadSizeLimit = configer.get("uploadSizeLimit");
    const config = { defaultUserSpace, uploadSizeLimit }
    res.json(config);
}

const updateConfig = async (req, res) => {
    const { defaultUserSpace, uploadSizeLimit } = req.body;
    if (Number.isNaN(defaultUserSpace) || Number.isNaN(uploadSizeLimit)) {
        return res.status(400).json({ message: "修改的大小必须是数字" });
    }
    if (defaultUserSpace < 0 || uploadSizeLimit < 0) {
        return res.status(400).json({ message: "修改的大小过小" });
    }
    configer.set("defaultUserSpace", Number(defaultUserSpace));
    configer.set("uploadSizeLimit", Number(uploadSizeLimit));
    res.json({ message: "更新成功" });
}

export default { getTenUsers, getUsers, getOverview, getRecentDays, getFileProperty, getLogs, updateSpace, updateUserStatus, updateUserRole, getConfig, updateConfig };
